package email

import (
	"errors"
	"github.com/sirupsen/logrus"
)

type emailStore struct {
	Email string
	Code  string
}

var EmailStoreMap = map[string]emailStore{}

func Set(emailID, email, code string) {
	EmailStoreMap[emailID] = emailStore{
		Email: email,
		Code:  code,
	}
}

// 自定义验证
func Verify(emailID, email, code string, clear bool) (bool, error) {
	store, ok := EmailStoreMap[emailID]
	if clear {
		defer delete(EmailStoreMap, emailID)
	}
	if !ok {
		return false, errors.New("请先申请验证码")
	} else if store.Email != email {
		return false, errors.New("绑定的邮箱错误，请输入验证码绑定的邮箱")
	} else if store.Code != code {
		logrus.Info(store.Code, code)
		return false, errors.New("验证码错误")
	}

	return true, nil

}
